local ClientOHOH =_G.ClientOHOH
local M = {}
local shopPanel = nil
local CallBackManager = ClientOHOH.load.CallBackManager()
local ConnectClass=_G.CommonOHOH.load.Connect();
M.UseEvent=ConnectClass.new()
local UiData = {}
local listName = {
    "damage",
    "diamond",
    "exp",
    "food",
    "health",
    "lucky",
    "wins"
}
function Initsts()
    local ui = {
        list = {},
        collection = {
            cell = "ctrl.potions.collection.btn",
            numtext = "ctrl.potions.collection.num",
            price = "ctrl.potions.collection.purchase",
            descText = "ctrl.potions.collection.text"
        },
        gSideTag = "ctrl.sideBar.potions",
        potionRedDot = "ctrl.sideBar.potionsRed",
        RedCount = "ctrl.sideBar.potionsCount"
    }
   
    for _, value in pairs(listName) do
        ui.list[value] = {
            buyBtn = "ctrl.potions."..value..".buyBtn",
            buyNum = "ctrl.potions."..value..".buyNum",
            price = "ctrl.potions."..value..".price",
            useBtn = "ctrl.potions."..value..".usebtn",
            useNum = "ctrl.potions."..value..".useNum",
            detailsText = "ctrl.potions."..value..".details",
        }
    end
    return ui
end

function M.GetInitUiList()
    return Initsts()
end

function M.Init(ShopPanel)
    shopPanel = ShopPanel
    M.SetPositionRed()
    CallBackManager:AddRbxConnect(shopPanel,shopPanel.insts.potions.gSideTag.Activated,function()
        M.SetPositionRed()
        shopPanel:ScrollingGo(shopPanel.insts.potions.gSideTag.X.Value)
    end)
end

function M.SetPositionRed(self,count)
    if not count then
		shopPanel.insts.potions.RedCount.Text="0"
		shopPanel.insts.potions.potionRedDot.Visible=false
	else
		shopPanel.insts.potions.potionRedDot.Visible=true
		local oldCount=tonumber(shopPanel.insts.potions.RedCount.Text)
		shopPanel.insts.potions.RedCount.Text=tostring(count+oldCount) 
	end
end

function M.Update()
    for _,cell in pairs(UiData) do
        cell:update()
    end
end

function M.Full(data)
    for index, value in pairs(data) do
        if #value.des ~= 1 then
            M.FullPotionPack(value)
        else
            local list = string.split(value.item_id,"_")
            M.FullPotionList(value,list[2])
        end
    end
end

function M.FullPotionPack(data)
    local potionUi  = shopPanel.insts.potions
    potionUi.collection.numtext.Text = "X"..data.count
    potionUi.collection.price.Text = data.price
    shopPanel:FullText(potionUi.collection.descText,{data.count})
    CallBackManager:AddRbxConnect(shopPanel,potionUi.collection.cell.Activated,function()
        shopPanel.ShopBuyEvent:Action(data.buyInfo)
    end)
end

function M.FullPotionList(data,index)
    local potionUi  = shopPanel.insts.potions
    potionUi.list[index].price.Text = data.price
    potionUi.list[index].buyNum.Text ="x".. data.sellCount
    shopPanel:FullText(potionUi.list[index].useNum,{data.had})
    shopPanel:FullText(potionUi.list[index].detailsText,{data.descArg})
    CallBackManager:AddRbxConnect(shopPanel,potionUi.list[index].buyBtn.Activated,function()
        shopPanel.ShopBuyEvent:Action(data.buyInfo)
    end)
    CallBackManager:AddRbxConnect(shopPanel,potionUi.list[index].useBtn.Activated,function()
        M.UseEvent:Action(data)
    end)

    table.insert(UiData,{
        ui=potionUi.list[index],
        cfg=data,
        update=function(this)
            shopPanel:FullText(this.ui.useNum,{data.had})
        end
    })

end

function M.Get()
    return M
end

return M 